package com.zhy.config;

import com.zhy.advice.ExceptionHandlerAdvice;
import com.zhy.interceptor.DemoInterceptor;
import com.zhy.messageconverter.MyMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.util.List;

/**
 * springmvc配置类
 * 定制配置的话就需要继承WebMvcConfigurerAdapter，重写这个类的方法，完成我们的常用配置
 * Created by zhy on 2017/5/3.
 */
@Configuration
//开启一些默认配置，如一些ViewReslover或者MessageConverter等,若无此句，重写WebMvcConfigurerAdapter方法无效
@EnableWebMvc
@ComponentScan("com.zhy.mvc, com.zhy.service")
@EnableScheduling
public class MyMvcConfig extends WebMvcConfigurerAdapter{

    /**
     * 配置一个JSP的ViewResolver，用来映射路径和实际页面位置
     * @return
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        System.out.println("MyMvcConfig is to load ....");
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        //跟开发看到的目录不一致，是因为运行时代码会将我们的页面自动编译到/WEB-INF/classes/views下
        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    @Bean
    public DemoInterceptor demoInterceptor() {
        return new DemoInterceptor();
    }

    @Bean
    public ExceptionHandlerAdvice exceptionHandlerAdvice() {return new ExceptionHandlerAdvice();}

    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(1000000);
        return multipartResolver;
    }

    /**
     * 注册自定义的HttpMessageConverter
     */
    @Bean
    public MyMessageConverter converter() {return new MyMessageConverter();}

    /**
     * addResourceHandler指的是对外暴露的访问路径
     * addResourceLocations指的是文件放置的目录
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor());
    }

    /**
     * 简化配置用的，比如一个controller里面有个index方法什么都不干，只返回到index页面，如果这种方法多的话，会很麻烦
     * 所以就可以在配置类里面重写addViewControllers这个方法来绑定路径和页面
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("/index");
        registry.addViewController("/toUpload").setViewName("/upload");
        registry.addViewController("/converter").setViewName("/converter");
        registry.addViewController("/sse").setViewName("/sse");
        registry.addViewController("/async").setViewName("/async");

    }

    /**
     * 重写该方法可以不忽略url最后“。”后面的参数
     * 比如http://localhost:8080/anno/pathvar/xx.yy   重写了该方法就不会忽略yy了
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

    /**
     * 在SpringMvc里注册HttpMessageConverter有两个方法
     * configureMessageConverters:重载会覆盖掉SpringMvc默认注册的多个HttpMessageConverter
     * extendMessageConverters仅添加一个自定义的HttpMessageConverter，不覆盖默认注册的HttpMessageConverter
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
    }
}
